package com.viknix.threadpool.manager.http.handler;

import com.viknix.threadpool.manager.http.Header;
import com.viknix.threadpool.manager.http.response.HttpClientResponse;
import com.viknix.threadpool.manager.http.result.HttpRestResult;
import com.viknix.threadpool.manager.server.dependence.util.JacksonUtils;

import java.io.InputStream;
import java.lang.reflect.Type;

/**
 * @Author: Dongqi
 * @Date: 2021/11/22 10:57
 * @Version 1.0
 * @Description: bean response handler,Mainly converter response type as bean type.
 */
public class BeanResponseHandler<T> extends AbstractResponseHandler<T> {

    @Override
    public HttpRestResult<T> convertResult(HttpClientResponse response, Type responseType) throws Exception {
        final Header headers = response.getHeaders();
        InputStream body = response.getBody();
        T extractBody = JacksonUtils.toObj(body, responseType);
        return new HttpRestResult<T>(headers, response.getStatusCode(), extractBody, null);
    }
}
